<?php

namespace App\Http\Controllers\Domain;


use App\Http\Controllers\Controller;
use App\Helper;
use App\Http\Requests\PlayerChangePwdRequest;
use App\Models\Character;
use App\Models\Server;
use App\Models\Video;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Response;

class HomeController extends Controller
{
    public $prefix='domain';

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Contracts\Support\Renderable
     */
    public function index($domain)
    {
        $server=Server::where('domain',$domain)->first();
//        Lib::setDataBaseConf('mysql_char',$server,'acore_characters');

//        $dsn = "mysql:host=".$server->db_host.";dbname=auth";
//        $db = new PDO($dsn, $server->db_acc, $server->db_pwd);
//        $con = new Connection($db);
//
//        $adminIds=[];
//        if (config('conf.showAdminCharacter')==0){
//            $r = $con->select("select id from account_access where gmlevel>=3");
//            foreach ($r as $item){
//                $adminIds[]=$item->id;
//            }
//        }

//        $characters=Character::where('logout_time','>','(unix_timestamp(now()) -2592000)')->whereNotIn('account',$adminIds)->where('level','>',config('conf.showMinLevel'))->orderBy('online','desc')->orderBy('level','desc')->get();
//        $characters=Character::where('logout_time','>','(unix_timestamp(now()) -2592000)')->where('level','>',config('conf.showMinLevel'))->orderBy('online','desc')->orderBy('level','desc')->get();
//        $characters=[];
//        $array_race=['1'=>0, '2'=>0, '3'=>0, '4'=>0, '5'=>0, '6'=>0, '7'=>0, '8'=>0, '10'=>0, '11'=>0];
//        $array_class=['1'=>0, '2'=>0, '3'=>0, '4'=>0, '5'=>0, '6'=>0, '7'=>0, '8'=>0, '9'=>0, '11'=>0];
//
//        foreach ($characters as  $c) {
//            $array_race[$c->race]++;
//            $array_class[$c->class]++;
//        }

//        return $server->endTime.' - '.now() .' - '.($server->endTime < now()?1:0) .' - '.(config('conf.serverFree')?1:0);
        if ($server->enable){
            return view($this->prefix.'.index'.$server->template,compact('server'));
        }else{
            return '此服务器已经停止运营! 如有疑问，请联系客服：'.$server->qq_server;
        }
    }
    public function client($domain)
    {
        $server=Server::where('domain',$domain)->first();
        Helper::setDataBaseConf('mysql_auth',$server,'acore_auth');

        return view($this->prefix.'.client',compact('server'));
    }
    public function dlq($domain)
    {
        $server=Server::where('domain',$domain)->first();
        if(isset($server->ext['loginUrl'])){
            return redirect($server->ext['loginUrl']);
        }

        $txt = '@echo y | rd /s "Cache"
echo SET realmlist "'.$server->server_host.($server->auth_port==3724||$server->auth_port==8085?'':':'.$server->auth_port).'"> realmlist.wtf
echo SET realmList "'.$server->server_host.($server->auth_port==3724||$server->auth_port==8085?'':':'.$server->auth_port).'"> Data/zhCN/realmlist.wtf
echo SET realmList "'.$server->server_host.($server->auth_port==3724||$server->auth_port==8085?'':':'.$server->auth_port).'"> Data/zhTW/realmlist.wtf

ren Data\commoo.MPQ common.MPQ
ren Data\expansioo.MPQ expansion.MPQ
start wow.exe
goto end
:end';

        $headers = ['Content-type' => 'text/plain', 'Content-Disposition' => sprintf('attachment; filename="登录器-'.$server->name.'.bat"')];
        return Response::make($txt, 200, $headers);
    }

    public function rank($domain)
    {
        $server=Server::where('domain',$domain)->first();
        Helper::setDataBaseConf('mysql_char',$server,'acore_characters');
        $chars=Character::orderBy('logout_time','desc')->paginate(20);

        $count=[];
        $count['LM']=Character::whereIn('race',[1,3,4,7,11])->count();
        $count['BL']=Character::whereIn('race',[2,5,6,8,10])->count();

        return view($this->prefix.'.rank',compact('server','chars','count'));
    }

    public function config($domain){
        $server=Server::where('domain',$domain)->first();
        $ext=$server->ext;
        if ($ext){
            foreach ($ext as $k=>$v){
                if(in_array($k,config('config-hide'))){
                    unset($ext[$k]);
                }
            }
        }else{
            $ext=[];
        }

        $conf=$server->conf;
        if ($conf) {
            foreach ($conf as $k => $v) {
                if (in_array($k, config('config-hide'))) {
                    unset($conf[$k]);
                }
            }
        }
        $luaList=$server->luaList();
        foreach ($luaList as $k=>$v){
            if(
                !strpos($v['name'],'测试')===false ||
                !strpos($v['name'],'test')===false  ||
                !strpos($v['name'],'卸载')===false
            ){
                unset($luaList[$k]);
            }
        }
        return view($this->prefix.'.config',compact('server','ext','conf','luaList'));
    }

    public function video($domain){
        $server=Server::where('domain',$domain)->first();
        $list=Video::where('server_id',$server->id)->where('sort','>',0)->orderBy('sort','desc')->paginate(12);
        return view($this->prefix.'.video',compact('server','list'));

    }

    public function getSpellInfo(Request $request){
        $id=$request->get('id');
        $str=file_get_contents('http://db.wowmanage.cn/?spell='.$id.'&domain=cn&power');
        $str=$this->cut('{','}',$str);
        $arr=json_decode('{'.$str.'}');
        return JsonResponse::create(['err'=>0,'data'=>$arr]);
    }
    function cut($begin,$end,$str){
        $b = mb_strpos($str,$begin) + mb_strlen($begin);
        $e = mb_strpos($str,$end) - $b;
        return mb_substr($str,$b,$e);
    }

}
